import db from "utils/localstorage";

export default {
  namespaced: true,
  state: {
    // 文件系统的前缀url
    filePreURL: `${window.location.protocol}//${window.location.hostname}/purchase/`,
    tableRowHeight: 36, // 全局表格的行高
    // globalSpinning: false, // 全局页面的loading，放在<spin></spin>
    showTableRecordsNum: 7, // 在768屏幕下展示7条数据
    showTableRecordsNumMore: 10, // 在大屏幕下展示10条数据
    sidebar: {
      opened: true
    },
    settingBar: {
      opened: false
    },
    isMobile: false,
    theme: db.get("THEME", "dark"),
    layout: db.get("LAYOUT", "side"),
    systemName: "蓝得系统管理后台",
    systemNameEn: "",
    companyName: "蓝得系统",
    companyNameEn: "NEW ENERGY VEHICLE",
    copyright: `版权所有`,
    multipage: getBooleanValue(db.get("MULTIPAGE"), false),
    fixSiderbar: getBooleanValue(db.get("FIX_SIDERBAR"), true),
    fixHeader: getBooleanValue(db.get("FIX_HEADER"), true),
    colorList: [
      "rgb(245, 34, 45)",
      "rgb(250, 84, 28)",
      "rgb(250, 173, 20)",
      "rgb(66, 185, 131)",
      "rgb(82, 196, 26)",
      "rgb(24, 144, 255)",
      "rgb(47, 84, 235)",
      "rgb(114, 46, 209)"
    ],
    color: db.get("COLOR", "rgb(24, 144, 255)")
  },
  mutations: {
    setFilePreURL(state, val) {
      state.filePreURL = val;
    },
    setTableRowHeight(state, val) {
      state.tableRowHeight = val;
    },
    /* setGlobalSpinning(state, val) {
      state.globalSpinning = val;
    }, */
    setDevice(state, isMobile) {
      state.isMobile = isMobile;
    },
    setTheme(state, theme) {
      db.save("THEME", theme);
      state.theme = theme;
    },
    setLayout(state, layout) {
      db.save("LAYOUT", layout);
      state.layout = layout;
    },
    setMultipage(state, multipage) {
      db.save("MULTIPAGE", multipage);
      state.multipage = multipage;
    },
    setSidebar(state, type) {
      state.sidebar.opened = type;
    },
    fixSiderbar(state, flag) {
      db.save("FIX_SIDERBAR", flag);
      state.fixSiderbar = flag;
    },
    fixHeader(state, flag) {
      db.save("FIX_HEADER", flag);
      state.fixHeader = flag;
    },
    setSettingBar(state, flag) {
      state.settingBar.opened = flag;
    },
    setColor(state, color) {
      db.save("COLOR", color);
      state.color = color;
    }
  }
};

function getBooleanValue(value, defaultValue) {
  if (Object.is(value, null)) {
    return defaultValue;
  }
  if (JSON.stringify(value) !== "{}") {
    return value;
  } else {
    return false;
  }
}
